package giftmail.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import giftmail.model.User;
import giftmail.service.IUserService;
import giftmail.staticentity.StaticEntity;
import giftmail.util.Page;
import giftmail.util.SGConstant;
import giftmail.util.SGException;

@Controller
@RequestMapping(value = "/sysController")
public class UserController {

	@Autowired
	private IUserService userService;

	/**
	 * 查看User表List
	 * 
	 * @author demo
	 */
	@ResponseBody
	@RequestMapping(value = "/findUser", method = { RequestMethod.GET })
	public StaticEntity<User> findUser() {

		try {
			StaticEntity<User> en = new StaticEntity<User>();
			List<User> list = userService.findUser();
			en.setContentList(list);
			en.setCode(SGConstant.SUCCESS_CODE);
			return en;
		} catch (SGException e) {
			throw new SGException(e.getMessage());
		} catch (Exception e) {
			throw new SGException(e.getMessage());
		}
	}

	/**
	 * 查看User表List分页
	 * 
	 * @author demo
	 */
	@ResponseBody
	@RequestMapping("/findUserListByPage/{currentPage}")
	public ModelAndView findUserListByPage(User user, @PathVariable int currentPage) {

		// try {
		// StaticEntity<User> en = new StaticEntity<User>();

		ModelAndView view = new ModelAndView();

		Page page = new Page();
		page.setCurrentPage(currentPage);

		List<User> list = userService.findUserListByPage(user, page);

		view.addObject("userListByPage", list);
		view.addObject("page", page);
		view.setViewName("userView");

		// en.setContentList(list);
		// en.setCode(SGConstant.SUCCESS_CODE);
		// return en;
		return view;

		// } catch (SGException e) {
		// throw new SGException(e.getMessage());
		// } catch (Exception e) {
		// throw new SGException(e.getMessage());
		// }
	}

	/**
	 * 根据id查询对象
	 * 同时:插入redis
	 * @author demo
	 */
	@ResponseBody
	@RequestMapping(value = "/findUserById", method = { RequestMethod.GET })
	public String findUserById(Integer id, ModelMap model) {

		User user = userService.getById(id);
		model.put("User", user);
		
		return "/user";		//页面user.jsp 
	}
	
	/**
	 * 用户更新(传入原用户id,原用户name,新password三个属性,修改password)
	 * 同时:删除redis
	 * @author demo
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.GET })
	public String update(HttpServletRequest request,ModelMap model) {
		Integer id = null;
        if (request.getParameter("id") != null && !"".equals(request.getParameter("id"))) {
            id = Integer.valueOf(request.getParameter("id"));
        }
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setPassword(password);
        
		userService.update(user);
		//model.put("User", update);
		return "/user";		//页面user.jsp 
	}
	
}
